package com.fastCms.business.frame.config;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * 杭州人参大健康管理有限公司 版权所有 © Copyright 2019<br>
 *
 * @Description: fastJson字段处理-保留两位小数<br>
 * @Project: healthyPro <br>
 * @CreateDate: Created in 2019/4/15 下午5:36 <br>
 * @Author: <a href="lsquan@healthpro.vip">lsquan</a>
 */
public class BigDecimalValueFilter extends JsonSerializer<BigDecimal> {

	// BigDecimal数字格式化
	private final static DecimalFormat df = new DecimalFormat("0.##");

	@Override
	public void serialize(BigDecimal bigDecimal, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
			throws IOException, JsonProcessingException {
		if (bigDecimal != null) {
			bigDecimal.setScale(2);

			jsonGenerator.writeString(df.format(bigDecimal));
		}
		// // 保留两位小数
//            BigDecimal bigDecimal = ((BigDecimal) fileValue).setScale(2);
//            return bigDecimal.toString();
//        }
//        return fileValue;
	}
}
